package com.zhuss.aop;

import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import com.zhuss.common.ResponseSucc;


/**
 * 请求响应格式统一包装返回给FE
 * @author zhuss
 * @since 2019-11-27 10:08
 */

@ControllerAdvice(basePackages = "com.zhuss.controller")
public class ResponseFilter implements ResponseBodyAdvice<Object> {

	public boolean supports(MethodParameter returnType,
			Class<? extends HttpMessageConverter<?>> converterType) {
		return true;
	}

	public Object beforeBodyWrite(Object body, MethodParameter returnType,
			MediaType selectedContentType,
			Class<? extends HttpMessageConverter<?>> selectedConverterType,
			ServerHttpRequest request, ServerHttpResponse response) {
		if (body instanceof ModelAndView || body instanceof ResponseSucc) {
			return body;
		}
		return new ResponseSucc(body);
	}
	   
	

}
